package com.cu.employmentmanagementsystem.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.cu.employmentmanagementsystem.common.exception.CommonException;
import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.dto.StudentVitaDTO;
import com.cu.employmentmanagementsystem.entity.Company;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.entity.StudentVita;
import com.cu.employmentmanagementsystem.entity.User;
import com.cu.employmentmanagementsystem.service.CompanyService;
import com.cu.employmentmanagementsystem.service.UserService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.PushBuilder;
import java.util.List;


@Api(tags = "公司相关操作")
@RequestMapping("/company")
@SaCheckLogin
@RestController
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private UserService userService;

    @ApiOperation("查询所有公司信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功，返回公司信息列表", response = List.class),
            @ApiResponse(code = 500, message = "服务器内部错误，查询失败")
    })
    @GetMapping("/getAllCompanies")
    public R<List<Company>> getAllCompanies(){
        try {
            return R.success(companyService.getAllCompanies());
        } catch (Exception e) {
            throw new CommonException("获取公司信息失败");
        }
    }
    @ApiOperation("查询所有招聘信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功，返回招聘信息列表", response = List.class),
            @ApiResponse(code = 500, message = "服务器内部错误，查询失败")
    })
    @RequestMapping("/queryRecruitments")
    public R<List<RecruitInformation>> queryRecruitments(){
        try {
            return R.success(companyService.queryRecruitments());
        } catch (Exception e) {
            throw new CommonException("获取招聘信息失败");
        }
    }

    @RequestMapping("queryInfo")
    @ApiOperation(value="通过userid查询公司信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功，返回公司信息", response = Company.class),
            @ApiResponse(code = 401, message = "用户未登录"),
            @ApiResponse(code = 500, message = "公司个体信息查询失败")
    })
    public R<Company> queryInfo(){
        try {
            String userAccount = StpUtil.getLoginIdAsString();
            if(userAccount == null) return R.error("非用户查询");
            User user = userService.getUserInfoByAccount(userAccount);
            Long id = user.getId();
            if(companyService.queryByCompanyId(id) != null){
                return R.success(companyService.queryByCompanyId(id));
            }else{
                return R.error("查询失败");
            }
        } catch (Exception e) {
            throw new CommonException("获取公司个体信息失败");
        }
    }

    @ApiOperation("根据招聘信息ID查询简历信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功，返回对应的简历信息列表", response = List.class),
            @ApiResponse(code = 404, message = "未找到对应的招聘信息或简历信息"),
            @ApiResponse(code = 500, message = "服务器内部错误，查询失败")
    })
    @RequestMapping("/queryResumesByRecruitmentId")
    public R<List<StudentVitaDTO>> queryResumesByRecruitmentId(Long recruitmentId){
        try {
            return R.success(companyService.queryResumesByRecruitmentId(recruitmentId));
        } catch (Exception e) {
            throw new CommonException("获取某项招聘对应的简历信息失败");
        }
    }

    @ApiOperation("根据招聘信息ID和简历ID接受简历投递状态")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功，已接受简历投递", response = Boolean.class),
            @ApiResponse(code = 404, message = "未找到对应的招聘信息或简历信息"),
            @ApiResponse(code = 500, message = "服务器内部错误，操作失败")
    })
    @RequestMapping("/acDeliveryStateByRecruitmentIdAndResumeId")
    public R<Boolean> acDeliveryStateByRecruitmentIdAndResumeId(Long recruitmentId, Long resumeId){
        try {
            return R.success(companyService.acDeliveryStateByRecruitmentIdAndResumeId(recruitmentId, resumeId));
        } catch (Exception e) {
            throw new CommonException("通过异常");
        }
    }

    @ApiOperation("根据招聘信息ID和简历ID拒绝简历投递状态")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功，已拒绝简历投递", response = Boolean.class),
            @ApiResponse(code = 404, message = "未找到对应的招聘信息或简历信息"),
            @ApiResponse(code = 500, message = "服务器内部错误，操作失败")
    })
    @RequestMapping("/refuseDeliveryStateByRecruitmentIdAndResumeId")
    public R<Boolean> refuseDeliveryStateByRecruitmentIdAndResumeId(Long recruitmentId, Long resumeId){
        try {
            return R.success(companyService.refuseDeliveryStateByRecruitmentIdAndResumeId(recruitmentId, resumeId));
        } catch (Exception e) {
            throw new CommonException("拒绝异常");
        }
    }

    @RequestMapping("updateInfo")
    @ApiOperation(value = "更新公司信息", notes = "根据提供的公司信息进行更新")
    @ApiParam(value = "公司信息", required = true)
    public R updateInfo(@ApiParam(value = "公司信息")
                        @RequestBody Company company){
        try{
            String userAccount = StpUtil.getLoginIdAsString();
            if(userAccount == null) return R.error("非用户查询");
            User user = userService.getUserInfoByAccount(userAccount);
            Long id = user.getId();
            company.setUserId(id);
            boolean result = companyService.update(company);
            //更新成功
            if(result){
                return R.success(result);
            }
            //更新失败
            else{
                return R.error("更新失败");
            }
        }catch (Exception e){
            throw new CommonException("更新公司信息的过程中出现异常");
        }
    }

}